﻿using System;
using System.Collections.Generic;

using System.IO;
using System.Text;

public partial class JE_XHTML : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

	private void restore(string filePath)
	{
		string folderPath = Path.GetDirectoryName(filePath);
		string backupFilePath = Path.Combine(folderPath, "..\\" + Path.GetFileNameWithoutExtension(filePath) + ".backup.xsd");

		if (File.Exists(backupFilePath))
			File.Copy(backupFilePath, filePath, true);

	}

	private void update(string filePath)
	{
		string folderPath = Path.GetDirectoryName(filePath);
		string backupFilePath = Path.Combine(folderPath, "..\\" + Path.GetFileNameWithoutExtension(filePath) + ".backup.xsd");

		if (!File.Exists(backupFilePath))
			File.Copy(filePath, backupFilePath, true);

		List<string> lines = new List<string>();

		foreach (string line in File.ReadAllLines(backupFilePath, Encoding.ASCII))
		{
			string newLine = string.Empty;

			if (line.IndexOf("<xsd:attribute name=\"Class\"") != -1)
				newLine = (line.Replace("name=\"Class\"", "name=\"je-class\""));
			if (line.IndexOf("<xsd:attribute name=\"Value\"") != -1)
				newLine = (line.Replace("name=\"Value\"", "name=\"je-value\""));
			else if (line.IndexOf("<xsd:attribute name=\"Id\" vs:nonfilterable=\"true\" vs:renametype=\"classmember\" vs:serverattribute=\"true\" />") != -1)
			{
				newLine = (line.Replace("<xsd:attribute name=\"Id\" vs:nonfilterable=\"true\" vs:renametype=\"classmember\" vs:serverattribute=\"true\" />", "<xsd:attribute name=\"je-id\" />"));
				newLine += ( line.Replace ( "<xsd:attribute name=\"Id\" vs:nonfilterable=\"true\" vs:renametype=\"classmember\" vs:serverattribute=\"true\" />", "<xsd:attribute name=\"je-template\" />" ) );
				newLine += ( line.Replace ( "<xsd:attribute name=\"Id\" vs:nonfilterable=\"true\" vs:renametype=\"classmember\" vs:serverattribute=\"true\" />", "<xsd:attribute name=\"je-button\" />" ) );
				newLine += ( line.Replace ( "<xsd:attribute name=\"Id\" vs:nonfilterable=\"true\" vs:renametype=\"classmember\" vs:serverattribute=\"true\" />", "<xsd:attribute name=\"je-datepicker\" />" ) );
				newLine += ( line.Replace ( "<xsd:attribute name=\"Id\" vs:nonfilterable=\"true\" vs:renametype=\"classmember\" vs:serverattribute=\"true\" />", "<xsd:attribute name=\"je-autocomplete\" />" ) );
				newLine += ( line.Replace ( "<xsd:attribute name=\"Id\" vs:nonfilterable=\"true\" vs:renametype=\"classmember\" vs:serverattribute=\"true\" />", "<xsd:attribute name=\"je-progressbar\" />" ) );
				newLine += ( line.Replace ( "<xsd:attribute name=\"Id\" vs:nonfilterable=\"true\" vs:renametype=\"classmember\" vs:serverattribute=\"true\" />", "<xsd:attribute name=\"je-slider\" />" ) );
			}
			else if (line.IndexOf("<xsd:attribute name=\"on") != -1 && line.IndexOf("\" vs:omtype=\"event\" />") != -1)
				newLine = (line.Substring(0, line.IndexOf("name=") + 6) + "je-" + line.Substring(line.IndexOf("name=") + 6));
			else if (line.IndexOf("<xsd:attribute name=\"Checked\"") != -1)
				newLine = (line.Replace("name=\"Checked\"", "name=\"je-checked\"").Replace(" vs:standalone=\"true\"", string.Empty).Replace(" type=\"disabledType\"", string.Empty));
			else if (line.IndexOf("<xsd:attribute name=\"Disabled\"") != -1)
				newLine = (line.Replace("name=\"Disabled\"", "name=\"je-disabled\"").Replace(" vs:standalone=\"true\"", string.Empty).Replace(" type=\"disabledType\"", string.Empty));
			else if (line.IndexOf("<xsd:attribute name=\"ReadOnly\"") != -1)
				newLine = (line.Replace("name=\"ReadOnly\"", "name=\"je-readonly\"").Replace(" vs:standalone=\"true\"", string.Empty).Replace(" type=\"readonlyType\"", string.Empty));
			else if (line.IndexOf("<xsd:attribute name=\"Selected\"") != -1)
				newLine = (line.Replace("name=\"Selected\"", "name=\"je-selected\"").Replace(" vs:standalone=\"true\"", string.Empty));
			else if (line.IndexOf("<xsd:attribute name=\"Multiple\"") != -1)
				newLine = (line.Replace("name=\"Multiple\"", "name=\"je-multiple\"").Replace(" vs:standalone=\"true\"", string.Empty));

			if (newLine != string.Empty)
			{

				if (newLine.EndsWith("\">"))
				{
					newLine = newLine.TrimEnd('>') + " />";
				}

				lines.Add(newLine);
			}

			lines.Add(line);
		}

		File.WriteAllLines(filePath, lines.ToArray(), Encoding.ASCII);
	}

	protected void cmdUpdate_Click(object sender, EventArgs e)
	{

		if (this.txtPath.Text == string.Empty)
		{
			this.lblInfo.Text = "路径不能为空";
		}
		else if (!Directory.Exists(this.txtPath.Text))
		{
			this.lblInfo.Text = "路径不存在";
		}
		else
		{
			this.update(Path.Combine(this.txtPath.Text, "commonHTMLTypes.xsd"));
			this.update(Path.Combine(this.txtPath.Text, "html_401.xsd"));
			this.update(Path.Combine(this.txtPath.Text, "IE6_0.xsd"));
			this.update(Path.Combine(this.txtPath.Text, "xhtml_frameset.xsd"));
			this.update(Path.Combine(this.txtPath.Text, "xhtml_strict-11.xsd"));
			this.update(Path.Combine(this.txtPath.Text, "xhtml_transitional.xsd"));

			this.lblInfo.Text = "更新完成!";
		}
		

	}

	protected void cmdRestore_Click(object sender, EventArgs e)
	{

		if (this.txtPath.Text == string.Empty)
		{
			this.lblInfo.Text = "路径不能为空";
		}
		else if (!Directory.Exists(this.txtPath.Text))
		{
			this.lblInfo.Text = "路径不存在";
		}
		else
		{
			this.restore(Path.Combine(this.txtPath.Text, "commonHTMLTypes.xsd"));
			this.restore(Path.Combine(this.txtPath.Text, "html_401.xsd"));
			this.restore(Path.Combine(this.txtPath.Text, "IE6_0.xsd"));
			this.restore(Path.Combine(this.txtPath.Text, "xhtml_frameset.xsd"));
			this.restore(Path.Combine(this.txtPath.Text, "xhtml_strict-11.xsd"));
			this.restore(Path.Combine(this.txtPath.Text, "xhtml_transitional.xsd"));

			this.lblInfo.Text = "<br>恢复完成!";
		}

	}

}